package com.neusoft.elmboot.controller;

import com.neusoft.elmboot.common.Result;
import com.neusoft.elmboot.dto.PermVo;
import com.neusoft.elmboot.dto.RoleReqDto;
import com.neusoft.elmboot.po.Role;
import com.neusoft.elmboot.service.PermissionService;
import com.neusoft.elmboot.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色表(Role)表控制层
 *
 * @author makejava
 * @since 2024-03-23 15:14:20
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    /**
     * 分页查询
     *
     * @param role 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public Result queryByPage(RoleReqDto role) {
        Map<String,Object> ret = new HashMap<>();
        List<Role> roleList = this.roleService.query(role);
        ret.put("data",roleList);
        ret.put("total",roleList.size());
        return Result.success(ret,"角色查询成功");
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Role> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.roleService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param role 实体
     * @return 新增结果
     */
    @PostMapping("/create")
    public Result add(@RequestBody RoleReqDto role) {

        return Result.success(this.roleService.insert(role),"角色新增成功");
    }

    /**
     * 编辑数据
     *
     * @param role 实体
     * @return 编辑结果
     */
    @PutMapping("/update")
    public Result edit(@RequestBody RoleReqDto role) {
        return Result.success(this.roleService.update(role),"更新角色成功");
    }

    /**
     * 删除数据
     *
     * @param reqDto 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/del")
    public Result deleteById(@RequestBody RoleReqDto reqDto) {
        Integer id = reqDto.getId();
        return Result.success(this.roleService.deleteById(id),"删除角色成功");
    }


    /**
     * 根据角色id来查询相应的权限信息。
     * @return
     */
    @GetMapping("/permissions")
    public Result getAllPerm(Integer roleId) {
        List<PermVo> permVos = permissionService.selectAllPermission();
        Map<String,Object> ret = new HashMap<>();
        ret.put("systemPermissions",permVos);
        ret.put("assignedPermissions",roleService.queryPermIdsByRoleId(roleId,permVos));

        return Result.success(ret,"获取权限信息成功");
    }


    @PostMapping("/permissions")
    public Result handlePermissions(@RequestBody RoleReqDto reqDto) {

        boolean result = roleService.savePermissions(reqDto.getRoleId(),reqDto.getPermissions());

        return Result.success(result,"设置权限成功");

    }


    @GetMapping("/roleList")
    public Result list() {
        List<Role> result = roleService.queryAllRole();
        return Result.success(result,"角色列表查询成功！");
    }
}

